package com.example.myapplication.lawyer;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.myapplication.Config.OKHttpTool;
import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.LawyerdetailsBean;
import com.example.myapplication.ShuJu.UserevaluationBean;
import com.example.myapplication.apter.UserevaluationApter;
import com.example.myapplication.apter.UserevaluationlawyerApter;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;

public class LawyerlistpageActivity extends AppCompatActivity {

    private String lawyerlistpageActivityID;
    private ImageView img_topchoicelawyy;
    private TextView name_topchoicelawyy;
    private TextView text_topchoicelawyy;
    private TextView year_topchoicelawyy;
    private TextView num_topchoicelawyy;
    private TabLayout tab_lawyerlistpage;
    private ImageView but_topchoicelawyy;
    private LawyerdetailsBean.DataBean data;
    private RecyclerView re_service;
    private RecyclerView re_user;
    private LawyerdetailsBean lawyerdetailsBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lawyerlistpage);
        initview();
        //返回
        but_bloak();
        //获取指定律师的详细信息
        lawyerdetails();

    }

    private void lawyerlistpagesort() {
        ArrayList<String> list = new ArrayList<>();
        list.add("服务方式");
        list.add("用户评价");
        for (int i = 0; i < list.size(); i++) {
            TabLayout.Tab tab = tab_lawyerlistpage.newTab();
            tab.setText(list.get(i));
            tab_lawyerlistpage.addTab(tab);
        }
        //处理分类
        userandhelpsort();
    }

    private void userandhelpsort() {
        tab_lawyerlistpage.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                String BaneNmae = tab.getText().toString();
                if ("服务方式".equals(BaneNmae)) {
                    servicemode();
                } else if ("用户评价".equals(BaneNmae)) {
                    userevaluation();
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    //用户评价
    private void userevaluation() {
        re_user.setVisibility(View.VISIBLE);
        re_service.setVisibility(View.INVISIBLE);
        Log.e("userevaluation", "userevaluation");
        //获取用用评价 lawyerlistpageActivityID
        OKHttpTool.doNetRequest("prod-api/api/lawyer-consultation/lawyer/list-evaluate?pageNum=1&pageSize=10&lawyerId=" + lawyerlistpageActivityID,
                OKHttpTool.GET,
                UserevaluationBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        Log.e("userevaluation", "onSuccess");
                        Log.e("run", "11111");
                        UserevaluationBean userevaluationBean = (UserevaluationBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (userevaluationBean.getCode() == 200) {
                                    UserevaluationlawyerApter userevaluationlawyerApter = new UserevaluationlawyerApter(LawyerlistpageActivity.this, userevaluationBean.getRows());
                                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(LawyerlistpageActivity.this);
                                    re_user.setLayoutManager(linearLayoutManager);
                                    re_user.setAdapter(userevaluationlawyerApter);

                                }
                            }
                        });

                    }

                    @Override
                    public void onFailure(String msg) {
                        Log.e("userevaluation", "onFailure");
                    }
                });
    }


    //服务方式
    private void servicemode() {
        re_user.setVisibility(View.INVISIBLE);
        re_service.setVisibility(View.VISIBLE);
        UserevaluationApter userevaluationApter = new UserevaluationApter(LawyerlistpageActivity.this, lawyerdetailsBean.getData());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(LawyerlistpageActivity.this);
        re_service.setAdapter(userevaluationApter);
        re_service.setLayoutManager(linearLayoutManager);
    }

    private void but_bloak() {
        but_topchoicelawyy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LawyerlistpageActivity.this.finish();
            }
        });
    }

    private void lawyerdetails() {
        OKHttpTool.doNetRequest("prod-api/api/lawyer-consultation/lawyer/" + lawyerlistpageActivityID,
                OKHttpTool.GET,
                LawyerdetailsBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        lawyerdetailsBean = (LawyerdetailsBean) obj;
                        if (lawyerdetailsBean.getCode() == 200) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    data = lawyerdetailsBean.getData();
                                    Glide.with(LawyerlistpageActivity.this).load(TokenBean.IP + data.getAvatarUrl()).into(img_topchoicelawyy);
                                    name_topchoicelawyy.setText(data.getName());
                                    text_topchoicelawyy.setText(data.getLegalExpertiseName());
                                    year_topchoicelawyy.setText(data.getWorkStartAt());
                                    num_topchoicelawyy.setText(String.valueOf(data.getServiceTimes()));
                                    name_topchoicelawyy.setText(data.getName());
                                    //服务方式
                                    servicemode();
                                    //分别是服务方式和用户评价
                                    lawyerlistpagesort();
                                }
                            });
                        }
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void initview() {
        Intent intent = getIntent();
        lawyerlistpageActivityID = intent.getStringExtra("LawyerlistpageActivityID");
        img_topchoicelawyy = findViewById(R.id.img_topchoicelawyy);
        name_topchoicelawyy = findViewById(R.id.Name_topchoicelawyy);
        text_topchoicelawyy = findViewById(R.id.text_topchoicelawyy);
        year_topchoicelawyy = findViewById(R.id.year_topchoicelawyy);
        num_topchoicelawyy = findViewById(R.id.num_topchoicelawyy);
        tab_lawyerlistpage = findViewById(R.id.tab_lawyerlistpage);
        but_topchoicelawyy = findViewById(R.id.but_topchoicelawyy);
        re_service = findViewById(R.id.re_service);
        re_user = findViewById(R.id.re_user);

    }
}